import config from '@/config'
import { ElMessage } from 'element-plus'

// 获取public下文件
export const getPublicFile = (url: string) => {
    return `${window.__MICRO_APP_PUBLIC_PATH__}${url}`
}

// 获取fpi-assets下的文件
export const getFpiAssetsFile = (url: string) => {
    return `${config.fpiAssetsRootPath}${url}?_v=${__APP_FPI_ASSETS_VERSION__}`
}

// 跳转到登陆页面
export function redirectLogin() {
    if (config.isDev) {
        window.open(config.baseAppConfig.loginURL)
    } else {
        window.location.href = config.baseAppConfig.loginURL
    }
}

// 设置token
export function setToken(token: string) {
    localStorage.setItem('token', token)
}

// 获取token
export function getToken() {
    const token = localStorage.getItem('token')
    return token
}

// 移除token
export function removeToken() {
    localStorage.removeItem('token')
}

// 统一提示配置
export const messageBox = (msg: string, type?: 'success' | 'error' | 'warning' | 'info') => {
    ElMessage({
        message: msg,
        type: type || 'error',
        duration: 2000,
    })
}

// 获取url上的的参数值
export const getUrlValue = (str = 'token') => {
    const url = location.href
    const uriSplit = url.split(`${str}=`)
    if (uriSplit[1]) {
        // 此时一定存在token
        const searchUrl = uriSplit[1].replace('#/', '')
        const firstAnd = searchUrl.indexOf('&')
        const token = searchUrl.slice(0, firstAnd > -1 ? firstAnd : searchUrl.length)
        return token
    }
    return null
}
